package com.advertisementsystem.Servie.Impl;

import com.advertisementsystem.Mapper.NotificationMapper;
import com.advertisementsystem.Servie.NotificationService;
import com.advertisementsystem.entity.Notification;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class NotificationServiceImpl  implements NotificationService {

    // extends ServiceImpl<NotificationMapper, Notification>
    @Autowired
    private NotificationMapper notificationMapper;


    @Override
    public IPage<Notification> getNotificationList(Page<Notification> page, Long userId, String type, Integer read) {
        return  notificationMapper.selectNotificationList(page, userId, type, read);
    }

    @Override
    @Transactional
    public Notification createNotification(Long userId, String title, String content,
                                           String type, Long targetId, String targetUrl) {
        LocalDateTime now = LocalDateTime.now();

        Notification notification = Notification.builder()
                .userId(userId)
                .title(title)
                .content(content)
                .type(type)
                // .read(0)  // 未读
                .targetId(targetId)
                .targetUrl(targetUrl)
                .createdTime(now)
                .updatedTime(now)
                .build();

        notificationMapper.insert(notification);
        return notification;
    }

    @Override
    @Transactional
    public Notification createNegotiationCompletedNotification(Long negotiationId, Long advertisementId,
                                                               Long receiverId, String senderName) {
        String title = "协商已完成";
        String content = String.format("您与 %s 的广告协商已成功完成", senderName);
        String targetUrl = "/schedule/detail/" + advertisementId;

        return createNotification(
                receiverId,
                title,
                content,
                "NEGOTIATION_COMPLETED",
                negotiationId,
                targetUrl
        );
    }

    @Override
    @Transactional
    public Notification createScheduleCanceledNotification(Long scheduleId, Long advertisementId,
                                                           Long receiverId, String senderName) {
        String title = "排期已取消";
        String content = String.format("您与 %s 的广告排期已被取消。", senderName);
        String targetUrl = "/schedule/list";

        return createNotification(
                receiverId,
                title,
                content,
                "SCHEDULE_CANCELED",
                scheduleId,
                targetUrl
        );
    }

    @Override
    @Transactional
    public boolean markAsRead(Long notificationId, Long userId) {

        Notification notification = notificationMapper.selectById(notificationId);

        // 验证消息是否存在且属于当前用户
        if (notification == null || !notification.getUserId().equals(userId)) {
            return false;
        }

        // 已经是已读状态
        if (notification.getRead() == 1) {
            return true;
        }

        // 更新为已读状态
        notification.setRead(1);
        notification.setUpdatedTime(LocalDateTime.now());

        return notificationMapper.updateById(notification)==1;
    }

    @Override
    @Transactional
    public boolean markAllAsRead(Long userId) {
        int updatedRows = notificationMapper.markAllAsRead(userId);
        return updatedRows > 0;
    }

    @Override
    @Transactional
    public boolean deleteNotification(Long notificationId, Long userId) {
        LambdaQueryWrapper<Notification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notification::getId, notificationId)
                .eq(Notification::getUserId, userId);

        return  notificationMapper.delete(wrapper)==1;
    }

    @Override
    public int getUnreadCount(Long userId) {
        return  notificationMapper.countUnreadNotifications(userId);
    }
}